import React from 'react'
import uuid from 'uuid'
import Form from '../../../share/Form'
import *as StringUtils from '../../../public/js/stringUtils'
import * as FormUtil from '../../../public/js/formUtil'
import * as AlertUtil from '../../../public/js/alertUtil'

class TaskEdit extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            formId : uuid.v4(),
            formData : {}
        }
    }

    componentDidMount() {
        const id = this.props.match.params['id']
        if(StringUtils.isNotEmpty(id)) {
            const url = domainName + '/replite/tasks/' + id, data = {}
            FormUtil.getData(url,data,(response)=>{
                FormUtil.loadJsonDataToForm($('#'+ this.state.formId), response)
            })
        }
    }

    save() {
        let url = domainName + '/replite/tasks', data = $('#' + this.state.formId).serializeObject(), id = this.props.match.params['id'],
        submitMethod = 'POST'
        if(StringUtils.isNotEmpty(id)) {
            submitMethod = 'PUT'
        } 
        FormUtil.submit(url, data, (response)=>{
            AlertUtil.alert('提示',"保存成功!",()=>{
                this.props.history.goBack()
            })
        },(error) => {
            AlertUtil.alert('提示',"保存失败,请联系管理员!")
        }, submitMethod)
        
    }

    cancel() {
        this.props.history.goBack()
    }

    render() {
        return (
            <div>
                <Form formId={this.state.formId} saveHandle={this.save.bind(this)} cancelHandle={this.cancel.bind(this)}>
                    <div className="form-group row">
                        <input type="hidden" name="id" />
                        <label className="col-sm-2 text-right control-label col-form-label">任务名称<span className="text-danger">*</span></label>
                        <div className="col-sm-10">
                            <input type="text" name="name" className="form-control" placeholder="名称" required maxLength="128"/>
                        </div>
                    </div>


                    <div className="form-group row">
                        <label className="col-sm-2 text-right control-label col-form-label">任务代码<span className="text-danger">*</span></label>
                        <div className="col-sm-10">
                            <input type="text" name="taskCode" className="form-control" placeholder="任务代码" maxLength="128"/>
                        </div>
                    </div>

                    <div className="form-group row">
                        <label className="col-sm-2 text-right control-label col-form-label">任务地址<span className="text-danger">*</span></label>
                        <div className="col-sm-10">
                            <input type="text" name="site" className="form-control" placeholder="任务地址" maxLength="512"/>
                        </div>
                    </div>
                </Form>            
            </div>
        )
    }
}


export default TaskEdit